<template>
  <el-dialog :visible="showDialog" :title="showTitle" width="30%" @close="close">
    <el-form ref="formRef" label-width="25%" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width: 80%" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width: 80%" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" style="width: 80%" size="mini" placeholder="1-10个字符">
          <el-option v-for="item in managers" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" :rows="4" style="width: 80%" size="mini" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="onSubmit">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { deptAddDepartmentApi, deptGetDepartmentsApi, deptGetManagersApi, deptUpdateDepartmentApi } from '@/api/department'

export default {
  name: 'DeptDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      defaultFromData: {
        id: null,
        pid: null,
        name: '',
        code: '',
        managerId: null,
        managerName: '',
        introduce: '',
        createTiem: ''
      },
      formData: {
        id: null,
        pid: null,
        name: '',
        code: '',
        managerId: null,
        managerName: '',
        introduce: '',
        createTiem: ''
      },
      rules: {
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码2-10个字符', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let res = this.departments

              if (this.formData.id) {
                res = res.filter((item) => item.id !== this.formData.id)
              }

              res.some(item => item.code === value) ? callback(new Error('部门编码已经存在')) : callback()
            }, trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍1-100个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称2-10个字符', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              let res = this.departments

              if (this.formData.id) {
                res = res.filter((item) => item.id !== this.formData.id)
              }
              res.some(item => item.name === value) ? callback(new Error('部门名称已经存在')) : callback()
            },
            trigger: 'blur'
          }
        ]
        // pid: []
      },
      departments: [],
      managers: [],
      loading: false
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '修改部门' : '新增部门'
    }
  },
  watch: {
    /**
     * 弹窗打开时，获取 部门列表 和 部门负责人列表
     */
    async showDialog(value) {
      if (value) {
        this.departments = await deptGetDepartmentsApi()
        this.managers = await deptGetManagersApi()
      }
    }
  },
  methods: {
    /**
     * 关闭弹层
     */
    close() {
      this.$emit('update:showDialog', false)
      this.$refs.formRef.resetFields()
      setTimeout(() => {
        this.formData = this.defaultFromData
      }, 200)
    },
    /**
     * 确认点击事件
     */
    async onSubmit() {
      await this.$refs.formRef.validate()

      if (this.formData.id) {
        await deptUpdateDepartmentApi(this.formData)
        this.$message.success('修改部门成功~')
      } else {
        await deptAddDepartmentApi({ ...this.formData, pid: this.currentNodeId })
        this.$message.success('新增部门成功~')
      }

      this.$emit('onSuccess')
      this.close()
    }
  }
}
</script>

<style scoped lang='less'>

</style>
